package com.wondertek.poms.core.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.IMoveContLogService;
import com.wondertek.poms.dao.po.MoveContLog;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * <p><b>PcProductRuleServiceImpl Description:</b> 二级产品规则 service层实现 </p>
 *
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-16 15:56:15</p>
 */
@Service
public class MoveContLogImpl extends ServiceImpl<MoveContLog, Long> implements IMoveContLogService {
	
	@PersistenceContext
    public EntityManager entityManager;

	@Override
	public MoveContLog deleteLogic(Long id) {
		return null;
	}

	@Override
	public MoveContLog merge(MoveContLog model) {
		return null;
	}
	
	@Override
	public Page<MoveContLog> findMoveLogList(MoveContLog MoveContLog, int page, int rows, String sord, String sidx) {
		StringBuilder condition = sqlStringWhere(MoveContLog);

        BigDecimal count = (BigDecimal) entityManager
				.createNativeQuery("select count(*) from ( "  + condition.toString() + " ) a").getSingleResult();
		Pageable pageable = getPageable(page - 1, rows, sidx, sord);
		if (count.longValue() > 0l) {
            StringBuilder conditionOrder = sqlStringOrder(sord, sidx);
            condition.append(conditionOrder);
            Query query = entityManager.createNativeQuery(condition.toString());
            query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
            query.setMaxResults(pageable.getPageSize());
            List<Object[]> resultList = query.getResultList();
            List<MoveContLog> list = getList(resultList);
            entityManager.close();
            return new PageImpl<>(list, pageable, count.longValue());
		}
		return Page.empty(pageable);
	}

	private StringBuilder sqlStringOrder(String sord, String sidx) {
		StringBuilder conditionOrder = new StringBuilder();
		if (StrUtil.isNotBlank(sidx)) {
			conditionOrder.append(" ORDER BY  " + sidx + "  " + sord);
		}
		return conditionOrder;
	}

    private StringBuilder sqlStringWhere(MoveContLog moveContLog) {
        StringBuilder condition = new StringBuilder();
        String sql = "SELECT  " +
                " pm.old_prd_info_id," +
                " pi2.name name," +
                " pm.new_prd_info_id," +
                " pi.name name1," +
                " pm.prd_cont_id," +
                " pm.content_id, " +
                " pm.create_time," +
                " pm.operator," +
                " pi.FIRST_PRDINFO_PACKAGE," +
                " pf.first_product_name," +
                " pi.SECOND_PRDINFO_PACKAGE," +
                " ps.product_info_package_name" +
                " FROM move_cont_log pm " +
                " LEFT JOIN prd_info pi ON pm.new_prd_info_id = pi.prdinfoid" +
                " LEFT JOIN prd_info pi2 ON pm.old_prd_info_id = pi2.prdinfoid " +
                " LEFT JOIN first_product_info_package pf ON pi.first_product_info_package_id = pf.first_product_id " +
                " LEFT JOIN product_info_package ps ON pi.product_info_package_id=ps.product_info_package_id where 1=1";

        condition.append(sql);

        if (moveContLog.getPrdContId() != null) {
            condition.append(" and pm.prd_cont_id = ").append("'" + moveContLog.getPrdContId() + "'");
        }
        if (moveContLog.getContentId() != null) {
            condition.append(" and pm.content_id = ").append("'" + moveContLog.getContentId() + "'");
        }
        if (moveContLog.getOldPrdInfoId() != null) {
            condition.append(" and pm.old_prd_info_id = ").append("'" + moveContLog.getOldPrdInfoId() + "'");
        }
        if (moveContLog.getNewPrdInfoId() != null) {
            condition.append(" and pm.new_prd_info_id = ").append("'" + moveContLog.getNewPrdInfoId() + "'");
        }
        if (moveContLog.getStartTime() != null) {
            condition.append(" and pm.created_time >= to_date (").append("'" + DateUtil.formatDateTime(moveContLog.getStartTime()) + "'").append(",'yyyy-MM-dd HH24:mi:ss')");
        }
        if (moveContLog.getEndTime() != null) {
            condition.append(" and pm.created_time <= to_date (").append("'" + DateUtil.formatDateTime(moveContLog.getEndTime()) + "'").append(",'yyyy-MM-dd HH24:mi:ss')");
        }
        return condition;
    }

    private List<MoveContLog> getList(List<Object[]> rows) {
        List<MoveContLog> list = new ArrayList<>();
        for (Object[] o : rows) {
            MoveContLog temp = new MoveContLog();
            temp.setOldPrdInfoId(o[0] != null ? (Convert.toLong(o[0])) : null);
            temp.setOldPrdInfoName(o[1] != null ? o[1].toString() : null);
            temp.setNewPrdInfoId(o[2] != null ? (Convert.toLong(o[2])) : null);
            temp.setNewPrdInfoName(o[3] != null ? o[3].toString() : null);
            temp.setPrdContId(o[4] != null ? (Convert.toLong(o[4])) : null);
            temp.setContentId(o[5] != null ? o[5].toString() : null);
            temp.setCreateTime(o[6] != null ? (Date) o[6] : null);
            temp.setOperator(o[7] != null ? o[7].toString() : null);
            temp.setFirstPrdInfoPackage(o[8] != null ? (Convert.toLong(o[8])) : null);
            temp.setFirstPrdInfoPackageName(o[9] != null ? o[9].toString() : null);
            temp.setSecondPrdInfoPackage(o[10] != null ? (Convert.toLong(o[10])) : null);
            temp.setSecondPrdInfoPackageName(o[11] != null ? o[11].toString() : null);
            list.add(temp);
        }
        return list;
    }


}